<template>
  <div class="login-body">
    <div class="login-con">
      <div class="head">
        <img
          class="logo"
          src="https://s.weituibao.com/1582958061265/mlogo.png"
        />
        <div class="name">
          <div class="title">新蜂商城</div>
          <div class="tips">Vue3.0 后台管理系统</div>
        </div>
      </div>

      <el-form
        label-position="top"
        class="login-form"
        :model="ruleForm"
        :rules="rules"
        ref="loginForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model.trim="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model.trim="ruleForm.password" ></el-input>
        </el-form-item>

        <el-form-item>
          <div style="color: #333">登录表示您已同意<a>《服务条款》</a></div>
          <el-button style="width: 100%" type="primary" @click="submitForm" >立即登录</el-button>
          <el-checkbox v-model="checked" @change="!checked">下次自动登录</el-checkbox>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import axios from '@/utils/axios'
import md5 from 'js-md5'
import { reactive, ref, toRefs } from 'vue'
import { localSet } from '@/utils'

export default {
  name: 'login',
  setup(){
    const loginForm = ref(null);
    

    const state = reactive({
      ruleForm: {
        username: '',
        password: ''
      },
      checked: true,
      rules: {
        username: [
          { required: 'true', message: '账户不能为空', trigger: 'blur' }
        ],
        password: [
          { required: 'true', message: '密码不能为空', trigger: 'blur' }
        ]
      }
    })


    
    const submitForm = () => {
      loginForm.value.validate((valid)=>{
          console.log(valid)
          if(valid) {
            axios.post('/adminUser/login',{
              userName: state.ruleForm.username || '',
              passwordMd5: md5(state.ruleForm.password)
            }).then(res=>{
              localSet('token', res)
              window.location.href = '/'
            })
            
          } else {
            console.log('error submit!!');
            return false;
          }
      })
    }

    return {
      ...toRefs(state), loginForm, submitForm
    }
  }
}

</script>

<style scoped>
.login-body {
  display: flex;
  width: 100vw;
  height: 100vh;
  align-items: center;
  justify-content: center;
}

.login-con {
  width: 420px;
  height: 500px;
  box-sizing: border-box;
  background-color: white;
  box-shadow: 0px 21px 41px 0px rgb(0 0 0 / 20%);
}

.head {
  display: flex;
  justify-content: center;
    align-items: center;
  padding: 40px 0 20px 
}

.logo {
  flex-basis: 100px;
  max-width: 100px;
  min-width: 100px;
  margin-right: 20px;
}

.login-form {
  width: 70%;
  margin: 0 auto;
}

.name {
  
}

.title {
      font-size: 28px;
    color: #1BAEAE;
    font-weight: bold;
}

.tips {
  font-size: 12px;
    color: #999;

}
</style>